#include "bootpack.h"

#define PIT_CTRL    0x0043
#define PIT_CNT0    0x0040

struct TIMERCTL timerctl;

#define TIMER_FLAG_UNUSE    0   /* 未使用 */
#define TIMER_FLAG_ALLOC    1   /* 已配置 */
#define TIMER_FLAG_USING    2   /* 运行中 */

void init_pit(void)
{
    struct TIMER *t;
    io_out8(PIT_CTRL, 0x34);
    io_out8(PIT_CNT0, 0x9c);
    io_out8(PIT_CNT0, 0x2e);
    timerctl.count = 0;
    for (int i = 0; i < MAX_TIMER; i++)
        timerctl.timers0[i].flag = TIMER_FLAG_UNUSE;
    /* 哨兵 */
    t = timer_alloc();
    t->timeout = 0xffffffff;
    t->flag = TIMER_FLAG_USING;
    t->next_timer = 0;
    timerctl.front = t;
    timerctl.next_time = 0xffffffff;
    return;
}

struct TIMER *timer_alloc(void)
{
    for (int i = 0; i < MAX_TIMER; i++)
    {
        if (timerctl.timers0[i].flag == 0)
        {
            timerctl.timers0[i].flag = TIMER_FLAG_ALLOC;
            timerctl.timers0[i].flag2 = TIMER_AUTOCANCEL_OFF;
            return &timerctl.timers0[i];
        }
    }
    return 0;
}

void timer_free(struct TIMER *timer)
{
    timer->flag = TIMER_FLAG_UNUSE;
    return;
}

void timer_init(struct TIMER *timer, struct FIFO32 *fifo, int data)
{
    timer->fifo = fifo;
    timer->data = data;
    return;
}

void timer_settime(struct TIMER *timer, unsigned int timeout)
{
    int e;
    struct TIMER *t, *s;
    timer->timeout = timeout + timerctl.count;
    timer->flag = TIMER_FLAG_USING;
    e = io_load_eflags();
    io_cli();
    t = timerctl.front;
    if (timer->timeout <= t->timeout)
    {
        /* 插入最前面 */
        timerctl.front = timer;
        timer->next_timer = t;
        timerctl.next_time = timer->timeout;
        io_store_eflags(e);
        return;
    }
    /* 搜索注册位置 */
    while (1)
    {
        s = t;
        t = t->next_timer;
        if (timer->timeout <= t->timeout)
        {
            /* 插入到s和t之间 */
            s->next_timer = timer;
            timer->next_timer = t;
            io_store_eflags(e);
            return;
        }
    }
    return;
}

int timer_cancel(struct TIMER *timer)
{
    int e;
    struct TIMER *t;
    e = io_load_eflags();
    io_cli();
    if (timer->flag == TIMER_FLAG_USING)
    {
        if (timer == timerctl.front)
        {
            /* 第一个定时器的取消处理 */
            t = timer->next_timer;

            timerctl.front = t;
            timerctl.next_time = t->timeout;
        }
        else
        {
            /* 非第一个定时器的取消处理 */
            t = timerctl.front;
            for (;;)
            {
                if (t->next_timer == timer)
                    break;
                t = t->next_timer;
            }
            t->next_timer = timer->next_timer;
        }
        timer->flag = TIMER_FLAG_ALLOC;
        io_store_eflags(e);
        return 1;   //sucessed
    }
    io_store_eflags(e);
    return 0;
}

void timer_cancelall(struct FIFO32 *fifo)
{
    int e;
    struct TIMER *t;
    e = io_load_eflags();
    io_cli();
    for (int i = 0; i < MAX_TIMER; i++)
    {
        t = &timerctl.timers0[i];
        if (t->flag != TIMER_FLAG_UNUSE && t->flag2 != TIMER_AUTOCANCEL_OFF &&
             t->fifo == fifo)
        {
            timer_cancel(t);
            timer_free(t);
        }
    }
    io_store_eflags(e);
    return;
}

void inthandler20(int *esp)
{
    char ts = 0;
    struct TIMER *timer;
    io_out8(PIC0_OCW2, 0x60);
    timerctl.count++;
    if (timerctl.next_time > timerctl.count)
        return; 
    timer = timerctl.front;
    for (;;)
    {
        if (timer->timeout > timerctl.count)
            break;
        /* timeout */
        timer->flag = TIMER_FLAG_ALLOC;
        if (timer != task_timer)
            fifo32_set(timer->fifo, timer->data);
        else
            ts = 1;
        timer = timer->next_timer;
    }
    /* 进行位移 */
    timerctl.front = timer;
    /* timerctl.next_time 的设定 */
    timerctl.next_time = timer->timeout;
    if (ts != 0)
        task_switch();
    return;
}
